﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace QuickCapture
{	
	/// <summary>
	/// Upload progress form
	/// </summary>
	public partial class UploadForm : Form
	{
		private FileHosting fileHosting;
		private Thread upload;
		private FileInfo fileInfo;
		private static int counter = 1;
		private string filePath;
		
		private string elegantFileSize(ref long bytes)
		{
			string[] prefixes = new string[4] { "B", "kB", "MB", "GB" };
			int prefixesPointer = 0;
			
			while (bytes > 1024)
			{
				if (prefixesPointer > 4)
					break;
				
				prefixesPointer++;
                bytes /= 1024;
			}
			
			return prefixes[prefixesPointer];
		}
		
		public UploadForm(string FilePath)
		{
			InitializeComponent();
			
			filePath = FilePath;
			
			// TODO: recognize extension
			
			HostingService imageshack = new HostingService("http://imageshack.us/", "fileupload", new Regex(@"(http://img\d+.imageshack.us/my.php\?image=[a-z0-9]+\.\w+)"));
			HostingService imgWklej = new HostingService("http://img.wklej.org/upload.php", "userfile", new Regex(@"(http://id.wklej.org/s/\d+)"));
			
			fileHosting = new FileHosting(imgWklej);
			fileHosting.OnProgress += new OnProgressHandler(fileProgress);
		}
		
		public UploadForm() : this(System.IO.Path.GetTempPath() + "ScreenCapture.jpg")
		{
			CaptureScreenForm cScreen = new CaptureScreenForm();
			cScreen.ShowDialog();
		}
		
		void UploadFormLoad(object sender, EventArgs e)
		{
			Text = "QuickCapture : " + Path.GetFileName(filePath);
			fileInfo = new FileInfo(filePath);
			
			upload = new Thread(new ThreadStart(performUpload));
			upload.SetApartmentState(ApartmentState.STA);
			upload.Start();
			tmrSpeed.Enabled = true;
		}
		
		private void performUpload()
		{
			try
			{
				fileHosting.UploadFile(filePath, null);
			}
			catch (System.Net.WebException e)
			{
				MessageBox.Show(e.Message, "QuickCapture Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			
			Clipboard.SetText(fileHosting.GetLink());
		}
		
		private void fileProgress(long bytesRead)
		{
			int percentage = (int)((bytesRead * 100)/fileInfo.Length);
			long fileSize = fileInfo.Length;
			long bytesReadCopy = bytesRead;
			
			string sentSizePrefix = elegantFileSize(ref bytesRead);
			string fileSizePrefix = elegantFileSize(ref fileSize);
			
			try { progressBar.Value = percentage; } catch (Exception e) { MessageBox.Show(e.Message); }
			
			lblStatus.Text = "Wysłano " + bytesRead.ToString() + " " + sentSizePrefix + " z " + fileSize.ToString() + " " + fileSizePrefix + " (" + percentage.ToString() + " %)";
			lblStatus.Text += " Prędkość: " + ((bytesReadCopy/counter)/1024).ToString() + "kB/s";
			
			if (percentage.CompareTo(100) == 0)
			{
                lblStatus.Text = "Zakończono, link do pliku znajduje sie w schowku.";
			}
		}
		
		void MainFormFormClosing(object sender, FormClosingEventArgs e)
		{
			try
			{
				upload.Abort();
				Application.Exit();
			}
			catch (Exception)
			{
				// TODO: What to do with it ?
			}
		}
		
		void TmrSpeedTick(object sender, EventArgs e)
		{
			counter++;
		}
	}
}
